<?php
include('database.php');

$offset = addslashes($_GET['offset']);
$limit = addslashes($_GET['limit']);
$token = addslashes($_GET['token']);

$list = getListComment('foody', 35140, $offset, $limit);
$response = array(
        'result' => true,
        'data' => array('list' => $list),
    );

echo json_encode($response);
exit;
 
function getListComment($chanel, $groupId, $offset=0, $limit=10)
{
    if($chanel == 'foody') {
        return getCommentFoody($groupId, $offset=0, $limit=10);
    }
}

function getCommentFoody($groupId, $offset=0, $limit=10)
{
    //$sql = "SELECT image,username,description,date_comment FROM `group_foody_comment` WHERE point <= 5 and group_id = " . $userGroup['group_id'];
    $sql = "SELECT image,username,description,date_comment FROM `group_foody_comment` WHERE group_id = " . $groupId . " order by time DESC limit $offset,$limit";
    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    $num_rows = mysql_num_rows($result);
    $data = array();
    if($num_rows > 0) {
        $i=0;
        while ($row = mysql_fetch_assoc($result)) {
            $data[$i]["image"] = $row["image"];
            $data[$i]["username"] = $row["username"];
            $data[$i]["title"] = 'Quan Nuong Ngoi - Nguyen Trai';
            $data[$i]["des"] = myTruncate($row["description"], 100);
            $data[$i]["date_comment"] = $row["date_comment"];
            
            $i++;
        }

        mysql_free_result($result);
    } else {
        //case not data
    }

    return $data;
}

function myTruncate($string, $limit, $break=".", $pad="...")
{
  // return with no change if string is shorter than $limit
  if(strlen($string) <= $limit) return $string;

  // is $break present between $limit and the end of the string?
    if(false !== ($breakpoint = strpos($string, $break, $limit))) {
        if($breakpoint < strlen($string) - 1) {
            $string = substr($string, 0, $breakpoint) . $pad;
        }
    }

    return $string;
}